package com.bw.task.fragment.onefragment;

import android.graphics.Color;
import android.graphics.Point;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bw.library.mvp.view.BaseFragment;
import com.bw.task.R;
import com.bw.task.adapter.MyLinearLayoutManager;
import com.bw.task.adapter.MyVideo;
import com.bw.task.bean.TestVideoUrl;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.shuyu.gsyvideoplayer.GSYVideoManager;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.List;


public class Fragment2 extends BaseFragment {
    private static final String TAG = "Fragment2";

    private RecyclerView recyler;
    boolean ifplay = true;
    MyVideo myPlayer3;

    @Override
    public int bandLayout() {
        return R.layout.fag_2;
    }

    @Override
    public void initData() {
        Log.i(TAG, "2initData: ");
        String[] videoUrls = TestVideoUrl.videoUrls;
        List<String> strings = Arrays.asList(videoUrls);
//        recyler.setLayoutManager(new LinearLayoutManager());
        recyler.setLayoutManager(new MyLinearLayoutManager(getContext()));
        MyAdapter myAdapter = new MyAdapter(R.layout.video, strings);
        recyler.setAdapter(myAdapter);
   /*     View decorView = getActivity().getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        getActivity().getWindow().setStatusBarColor(Color.TRANSPARENT);*/
        recyler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull @NotNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState){
                    case RecyclerView.SCROLL_STATE_IDLE:
                        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                        MyVideo viewById = layoutManager.getChildAt(0).findViewById(R.id.myVideo);
                        viewById.startPlayLogic();
                        break;
                }
            }
        });

    }

    @Override
    public void initView() {
        recyler = (RecyclerView) findViewById(R.id.recyler);
    }
    public class MyAdapter  extends BaseQuickAdapter<String, BaseViewHolder> {
        public MyAdapter(int layoutResId, @Nullable List<String> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder baseViewHolder, String video) {
            myPlayer3 = baseViewHolder.getView(R.id.myVideo);
            myPlayer3.setUp(video, true,"");

//           myPlayer3.startPlayLogic();
           myPlayer3.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    myPlayer3.startWindowFullscreen(getContext(),true,true);
                    myPlayer3.showSmallVideo(new Point(200,200),true,true);

                }
            });
            ImageView imageView = new ImageView(getContext());
            Glide.with(getContext()).load(R.drawable.mmt).into(imageView);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

            myPlayer3.setThumbImageView(imageView);
            if (ifplay){
                myPlayer3.onVideoPause();
            }
            ifplay=false;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i(TAG, "2onDestroyView: ");
    }

    @Override
    public void onPause() {
        super.onPause();
        GSYVideoManager.onPause();

        Log.i(TAG, "2onPause: ");
    }

    @Override
    public void onResume() {
        super.onResume();
//        GSYVideoManager.onResume();
        Log.i(TAG, "2onResume: ");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
        Log.i(TAG, "2onDestroy: ");
    }
}